// 数据可视化
import { defineStore } from "pinia";
import request from '@/utils/request'
import { ref } from 'vue'
export const useEchartsStore = defineStore('echartsStore', () => {
    const membersDataArr = ref({ keys: [], values: [] })
    const membersDataMap = ref([])
    // 获取各季度会员数量
    const getMembersCount = async () => {
        const { data } = await request.get('/user/members')
        const keys = Object.keys(data)
        membersDataArr.value.keys = keys
        const values = Object.values(data)
        membersDataArr.value.values = values
        fixPie(data)
    }

    function fixPie(data) {
        membersDataMap.value = []
        for (const d in data) {
            membersDataMap.value.push({
                value: data[d], name: d
            })
        }
    }

    return {
        membersDataArr,
        membersDataMap,
        getMembersCount
    }
}, {
    persist: {
        enabled: true,
        strategies: [{
            // 自定义存储的 key，默认是 store.$id
            key: "members",
            // 可以指定任何 extends Storage 的实例，默认是 sessionStorage
            storage: localStorage,
            // state 中的字段名，按组打包储存
            paths: ["membersData", "memberDataMap"]
        }],
    }
})